package com.nhim.cms.util;


import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.nhim.cms.dao.ContentDAO;
import com.nhim.cms.dao.ContentTypeElementDefinitionDAO;
import com.nhim.cms.dao.ContentVersionDAO;
import com.nhim.cms.dao.ContentVersionValueDAO;
import com.nhim.cms.domain.Content;
import com.nhim.cms.domain.ContentType;
import com.nhim.cms.domain.ContentTypeElementDefinition;
import com.nhim.cms.domain.ContentVersion;
import com.nhim.cms.domain.ContentVersionValue;
import com.nhim.cms.domain.Template;
import com.nhim.core.AppContext;
import com.nhim.core.AppSettings;
@Service
public class ContentVersionManager {

	@Autowired
	ContentVersionDAO contentVersionDAO;
	@Autowired
	ContentTypeElementDefinitionDAO contentTypeElementDefinitionDAO;
	@Autowired
	ContentVersionValueDAO contentVersionValueDAO;
	@Autowired
	ContentDAO contentDAO;
	@PostConstruct
	public void init() throws Exception {
	}

	@PreDestroy
	public void destroy() {
	}
	
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public  ContentVersion saveOrUpdate(ContentVersion entity)
			throws Exception {
		return contentVersionDAO.saveOrUpdate(entity);
	
	}
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void updateContentVersionValues(Set<ContentVersionValue> cvValues) throws Exception
	{
		for(ContentVersionValue value : cvValues)
			contentVersionValueDAO.saveOrUpdate(value);
	}
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public ContentVersion createNew(Content parent,String name,ContentType contentType,Template template) throws Exception
	{
		Content thisContent = new Content();
		thisContent.setArchived(false);
		thisContent.setCreateDate(new java.sql.Date(new java.util.Date().getTime()));
		thisContent.setVersion(1);
		thisContent = contentDAO.saveOrUpdate(thisContent);
		ContentVersion cv = new ContentVersion();
		cv.setContent(thisContent);
		cv.setContentType(contentType);
		cv.setCreateDate(new java.sql.Date(new java.util.Date().getTime()));
		cv.setCheckOutDate(new java.sql.Date(AppSettings.getInstance().getMaxDate().getTime()));
		cv.setDeactivateDate(new java.sql.Date(AppSettings.getInstance().getMaxDate().getTime()));
		cv.setFinalizedDate(new java.sql.Date(AppSettings.getInstance().getMaxDate().getTime()));
		cv.setIsLive(false);
		cv.setName(name);
		if(parent != null)
			cv.setParent(parent);
		cv.setPath(getPath(parent,name));
		cv.setTemplate(template);
		cv.setVersion(1);
		cv = saveOrUpdate(cv);
		List<ContentTypeElementDefinition> CTES = contentTypeElementDefinitionDAO.findByContentTypeId(contentType.getId());
		Set<ContentVersionValue> cvValues = new HashSet<ContentVersionValue>();
		for(ContentTypeElementDefinition CTE: CTES)
		{
			ContentVersionValue cvValue = new ContentVersionValue();
			cvValue.setContentVersion(cv);
			cvValue.setElementDefinition(CTE.getElementDefinition());
			cvValue.setVersion(1);
			cvValue.setValue("");
			cvValues.add(contentVersionValueDAO.saveOrUpdate(cvValue));
			
		}
		cv.setContentVersionValues(cvValues);
		return cv;
		
	}
	public String getPath(Content ct,String name)
	{
		return name;
		
	}
	public static ContentVersionManager getFromApplicationContext(
			ApplicationContext ctx) {
		return (ContentVersionManager) ctx
				.getBean(ContentVersionManager.class);
	}

	public static ContentVersionManager getInstance() {
		return getFromApplicationContext(AppContext.getApplicationContext());
		
	}

}
